
var provinceInfo={
		baseURL:"",
		divId:"test",
		provinceInfo:{},
		cityInfo:{},
		townInfo:{},
		provinceCode:-1,
		townCode:-1,
		cityCode:-1,
		empty:false,
		init:function(){
				this.provinceInfo=this.ajax(null,"_province","province");
				this.cityInfo=this.ajax(this.cityCode!=-1?this.provinceCode:-1,"_city","city");
				//this.townInfo=this.ajax(this.townCode!=-1?this.cityCode:-1,"_town","town");
				//$("#"+this.divId).html(this.provinceInfo.html+"&nbsp;"+this.cityInfo.html+"&nbsp;"+this.townInfo.html);	
				$("#"+this.divId).html(this.provinceInfo.html+"&nbsp;"+this.cityInfo.html);	
		},
		ajax:function(code,id,targetcode){
					if(code==null){
						code="province";
					}
					var html="";
					var str="";
					var rs;
					var html="<input class='combobox' id='"+id+"' name='' /><div class='"+id+"'><input type='text' value='' class='specialSelectInput' /><ul>";
					$.ajax({async:false, url: this.baseURL+"/api/interface/province/", data:"code="+code,dataType:"json",success:function(data){
								rs=data.results;
								for(var index=0;index<rs.length;++index){
									var info=rs[index];
									str+="<li id="+info.code+">"+info.name+"</li>";
								}
								html+=str+"</ul></div>";
					}});
					return {datas:rs,html:html,str:str,id:id};			
		},
		bindEvent:function(){
			$("#"+provinceInfo.divId).find("input.combobox:first").css('marginLeft','0');
			$("#"+provinceInfo.divId).find("input.combobox").on('keyup',function(e){
				$.each($(e.target).next().find('li'),function(i,n){
					if($(n).text().indexOf($(e.target).val())!=-1){
						$(n).show();
					}else $(n).hide();
				})
			})
			$("#"+provinceInfo.divId).find("input.combobox").on('click',function(e){
				var parentItem=$(e.target).prev().prev();
				if(parentItem.attr('name')!="" && parentItem.val()!=""){
					if(parentItem.attr('id')=='_province'){
						var cityInfo=provinceInfo.ajax(parentItem.attr('name'),"_city");
						$("#"+cityInfo.id).val("").next().find('ul').html(cityInfo.str).end().next().val("").next().find("ul").hide();
						if(provinceInfo.ajax($(e.target).next().find('li:first').attr("id"),"_city").datas.length==0){
							$('#_town').hide().next().hide()
							$('#_city').attr('placeholder','请选择区或盟');
						}else{
							$('#_town').show().attr('placeholder','请选择区').next().show();
							$('#_city').attr('placeholder','请选择市');
					    }
					}/*else if(parentItem.attr('id')=='_city'){
						var townInfo=provinceInfo.ajax(parentItem.attr('name'),"_town");
						$("#"+townInfo.id).val("").next().find('ul').show().html(townInfo.str);
					}*/	
				}
				if($(e.target).next().find('li').length==0){
					return false;
				}else $(e.target).next().show().siblings('div').hide();
			})
			$("#"+provinceInfo.divId).on('click','li',function(event){
					event.stopPropagation();
					var parentItem=$(event.target).parent().parent().prev();
					parentItem.val($(event.target).text()).attr('name',$(event.target).attr('id'));
					if(parentItem.attr('id')=="_province"){
						parentItem.siblings('input').val("");
					}
			})
			$("#"+provinceInfo.divId).on('mouseover','li',function(e){
					e.stopPropagation();
				    $(e.target).addClass('testColor').siblings().removeClass('testColor');
			})
			var isMouse=true;
			$('.combobox').on('click',function(e){
				e.stopPropagation();
				$(e.target).next().on('mouseleave',function(ev){
					ev.stopPropagation();
					$(e.target).next().hide();
					isMouse=true;
				})
				$(e.target).next().on('mouseover',function(ev){
					isMouse=false;
				})
			})
			$('.combobox').on('blur',function(e){
				if(isMouse){
					$(e.target).next().hide();
				}
			})
			$('#_province').attr('placeholder','请选择省');
			//$('#_town').attr('placeholder','请选择区');
			$('#_city').attr('placeholder','请选择市');
		}
};